<?php
/**
 *
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
 * @encoding UTF-8
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
class Message implements Page{
    public function main($args=NULL) {
        if (Session::getInstance()->sessionCheck()) {
            if (isset($args[0]) && $args[0]=='publish') $this->postMessage();
            else if (isset($args[0]) && !empty ($args[0])) $this->showMessagesWith($args[0]);
            else $this->showAllMessages();
        } else {
            header("Location: /index.php/");
        }
    }

    public function showAllMessages() {
        $messages = Storage::getInstance()->fetch()->query("SELECT Receiver as p FROM Messages WHERE Sender=".$_SESSION["id"]." UNION SELECT Sender as p FROM Messages WHERE Receiver=".$_SESSION["id"])->fetchAll(PDO::FETCH_ASSOC);

        $friendsData = array();
        foreach($messages as $f=>$v) {
            $info = Database::select("Compte", "id, pseudo, avatar", "id=".$v['p']);
            $friendsData[$info[0]['id']] = $info[0];
        }

        $S = Template::getSmarty();
        $S->assign("Me", false);
        $S->assign("FriendsData",$friendsData);
        $S->assign("UserID", $_SESSION['id']);
        $S->assign("UserPseudo", $_SESSION['pseudo']);
        $S->assign("UserAvatar", $_SESSION['avatar']);
        $S->display(Template::getPath('Messages_All'));

    }

    public function showMessagesWith($who) {
        $messages = Storage::getInstance()->fetch()->query("SELECT *, Receiver as p FROM Messages WHERE Sender=".$_SESSION["id"]." AND Receiver=".Database::q($who)." UNION SELECT *, Sender as p FROM Messages WHERE Receiver=".$_SESSION["id"]." AND Sender=".Database::q($who))->fetchAll(PDO::FETCH_ASSOC);

        foreach($messages as $f=>$v) {
            $info = Database::select("Compte", "id, pseudo, avatar", "id=".$v['p']);
            $friendsData[$info[0]['id']] = $info[0];
        }

        $name = Database::select("Compte", "pseudo", "id=".Database::q($who));
        $name = $name[0]['pseudo'];
        $S = Template::getSmarty();
        $S->assign("Me", true);
        $S->assign("Mess", $messages);
        $S->assign("FriendsData",$friendsData);
        $S->assign("Other", $name);
        $S->assign("UserID", $_SESSION['id']);
        $S->assign("UserPseudo", $_SESSION['pseudo']);
        $S->assign("UserAvatar", $_SESSION['avatar']);
        $S->assign("ToID", $who);
        $S->display(Template::getPath('Messages_One'));

    }

    public function postMessage() {
        if (POST("to") && POST('content') && Session::getInstance()->sessionCheck() && strlen($_POST['content']) < 301 && $_POST['to'] != $_SESSION['id']) {
            if(Database::insert("Messages", array('', $_SESSION['id'], $_POST['to'], $_POST['content'], ''))) {
                echo json_encode(array('status'=>"OK", 'date'=>  mktime()));
            } else {
                echo json_encode(array('status'=>"NOTOK"));
            }
        } else {
            echo json_encode(array('status'=>"NOTOK"));
        }
    }


    public function pseudoExist($pseudo) {
        return (count(Database::select("Compte", "pseudo", "pseudo=".Storage::getInstance()->fetch()->quote($pseudo) ) ) != 0);
    }
    
}
?>
